package com.scoy.honeymei.activity.mall;

import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.webkit.WebView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.Nullable;

import com.google.gson.Gson;
import com.lzy.okgo.model.HttpParams;
import com.scoy.honeymei.R;
import com.scoy.honeymei.activity.a_checkseaat.SeatActivity;
import com.scoy.honeymei.activity.login.LoginActivity;
import com.scoy.honeymei.base.BaseActivity;
import com.scoy.honeymei.bean.FilmOrderDetailBean;
import com.scoy.honeymei.http.HpCallback;
import com.scoy.honeymei.http.HpGo;
import com.scoy.honeymei.url.MeConstant;
import com.scoy.honeymei.url.MyUrl;
import com.scoy.honeymei.utils.GlideImgUtil;
import com.scoy.honeymei.utils.MyUtil;
import com.scoy.honeymei.utils.SPHelper;
import com.scoy.honeymei.utils.TimeUtil;
import com.yzq.zxinglibrary.android.CaptureActivity;
import com.yzq.zxinglibrary.bean.ZxingConfig;
import com.yzq.zxinglibrary.common.Constant;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * @ClassName: BookDetailActivity$
 * @Description: 订票详情
 * @Author: GaoZhan
 * @CreateDate: 2019/9/9/0009$ 上午 9:49$
 * @UpdateUser: 更新者
 * @UpdateDate: 2019/9/9/0009$ 上午 9:49$
 * @UpdateRemark: 更新内容
 */
public class BookDetailActivity extends BaseActivity {


    @BindView(R.id.back_iv)
    ImageView backIv;
    @BindView(R.id.title_tv)
    TextView titleTv;
    @BindView(R.id.sign_tv)
    TextView signTv;
    @BindView(R.id.title_llt)
    LinearLayout titleLlt;
    @BindView(R.id.bd_title_tv)
    TextView bdTitleTv;
    @BindView(R.id.bd_sign_iv)
    ImageView bdSignIv;
    @BindView(R.id.bd_tv0)
    TextView bdTv0;
    @BindView(R.id.bd_time_tv)
    TextView bdTimeTv;
    @BindView(R.id.bd_tv1)
    TextView bdTv1;
    @BindView(R.id.bd_hall_tv)
    TextView bdHallTv;
    @BindView(R.id.bd_tv2)
    TextView bdTv2;
    @BindView(R.id.bd_seat_tv)
    TextView bdSeatTv;



    @BindView(R.id.linearBtn)
    View linearBtn;
    @BindView(R.id.tpBtn)
    View tpBtn;
    @BindView(R.id.dydypBtn)
    View dydypBtn;




    @BindView(R.id.bd_qr_iv)
    ImageView bdQrIv;
    @BindView(R.id.bd_bg_view)
    View bdBgView;
    @BindView(R.id.bd_tv3)
    TextView bdTv3;
    @BindView(R.id.bd_num_tv)
    TextView bdNumTv;
    private BookDetailActivity mContext;
    private int fId;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_book_detail);
        ButterKnife.bind(this);
        mContext = BookDetailActivity.this;
        fId = getIntent().getIntExtra("fId", 0);
        initNormal();

        httpFilmDeatail();

    }

    @Override
    public void initNormal() {

        titleTv.setText(R.string.bookdetail);

        MyUtil.setStatusBar(mContext, getWindow(), true, R.color.colorWhite);
        titleLlt.setPadding(0, MyUtil.getStatusBarHeight(mContext), 0, 0);

    }

    @OnClick({R.id.back_iv, R.id.title_tv,R.id.tpBtn,R.id.dydypBtn})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.back_iv:
                finish();
                break;
            case R.id.title_tv:
                break;
            case R.id.tpBtn:
                //退票
                onNotice();
                break;
            case R.id.dydypBtn:
                //打印电影票
                startSaoYiSao();
                break;
        }
    }


    public void onNotice() {
        Dialog dialogNotice = new Dialog(mContext, R.style.BottomDialogStyle);
        View view = View.inflate(mContext, R.layout.dialog_tishi, null);
        TextView notTitleTv = view.findViewById(R.id.not_title_tv);
        TextView cancelBtn = view.findViewById(R.id.cancelBtn);
        TextView sureBtn = view.findViewById(R.id.sureBtn);
        notTitleTv.setText("提示");

        dialogNotice.setContentView(view);
        dialogNotice.setCanceledOnTouchOutside(true);
        Window dialogWindow = dialogNotice.getWindow();
        WindowManager.LayoutParams lp = dialogWindow.getAttributes();
        lp.width = (int) getResources().getDimension(R.dimen.dp_300);
        lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
        lp.gravity = Gravity.CENTER;
        dialogWindow.setAttributes(lp);
        cancelBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialogNotice.dismiss();
            }
        });
        sureBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                tpMethod();
                dialogNotice.dismiss();
            }
        });
        dialogNotice.show();
    }


    private void startSaoYiSao(){
        Intent intent = new Intent(this, CaptureActivity.class);
        ZxingConfig config = new ZxingConfig();
        config.setShowbottomLayout(true);//底部布局（包括闪光灯和相册）
        config.setPlayBeep(true);//是否播放提示音
        config.setShake(true);//是否震动
        config.setShowAlbum(true);//是否显示相册
        config.setShowFlashLight(true);//是否显示闪光灯
        intent.putExtra(Constant.INTENT_ZXING_CONFIG, config);
        startActivityForResult(intent, 1);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(resultCode == RESULT_OK){
            if(requestCode == 1){
                String no = data.getStringExtra(Constant.CODED_CONTENT);
                smdyMethod(no);
            }
        }
    }

    /**
     * 退票
     */
    private void tpMethod() {
        HttpParams params = new HttpParams();
        params.put("uid", SPHelper.getString(MeConstant.UID, ""));
        params.put("id", fId);

        HpGo.newInstance().HttpGo(this, MyUrl.FIIM_ORDER_TUI, params, new HpCallback() {
            @Override
            public void onSuccess(String o) {
                MyUtil.mytoast(mContext, "退票成功！");
                finish();
            }

            @Override
            public void onFailed(String code, String msg, String o) {
                MyUtil.mytoast(mContext, code + "," + msg);
            }

            @Override
            public void onError(int code, String errorMsg) {
                MyUtil.mytoast(mContext, errorMsg);
            }
        });

    }

    /**
     * 退票
     */
    private void smdyMethod(String no) {
        HttpParams params = new HttpParams();
        params.put("uid", SPHelper.getString(MeConstant.UID, ""));
        params.put("id", fId);
        params.put("no", no);
        HpGo.newInstance().HttpGo(this, MyUrl.FIIM_ORDER_DY, params, new HpCallback() {
            @Override
            public void onSuccess(String o) {
                MyUtil.mytoast(mContext, "打印成功！");
                finish();
            }

            @Override
            public void onFailed(String code, String msg, String o) {
                MyUtil.mytoast(mContext, code + "," + msg);
            }

            @Override
            public void onError(int code, String errorMsg) {
                MyUtil.mytoast(mContext, errorMsg);
            }
        });

    }

    private void httpFilmDeatail() {
        HttpParams params = new HttpParams();
        params.put("uid", SPHelper.getString(MeConstant.UID, ""));
        params.put("id", fId);

        HpGo.newInstance().HttpGo(this, MyUrl.FIIM_ORDER_DETAIL, params, new HpCallback() {
            @Override
            public void onSuccess(String o) {
                FilmOrderDetailBean filmBean = new Gson().fromJson((String) o, FilmOrderDetailBean.class);
                FilmOrderDetailBean.OrderBean order = filmBean.getOrder();
                bdTitleTv.setText(order.getMovie_title());
                bdNumTv.setText(order.getOrder_no());
                int status = order.getStatus();//状态，-1=已作废、0=未出票、1=已出票、-2=已作废
                if (status == -1 || status == -2 || status == 3) {
                    bdSignIv.setBackgroundResource(R.drawable.ic_bookall_abanden);
                    bdBgView.setVisibility(View.VISIBLE);
                    linearBtn.setVisibility(View.GONE);
                } else if (status == 1) {
                    bdSignIv.setBackgroundResource(R.drawable.ic_bookall_used);
                    bdBgView.setVisibility(View.VISIBLE);
                    linearBtn.setVisibility(View.GONE);
                } else {
                    bdSignIv.setBackgroundResource(R.drawable.ic_bookall_unuse);
                    bdBgView.setVisibility(View.GONE);
                    linearBtn.setVisibility(View.VISIBLE);
                }
                bdTimeTv.setText(TimeUtil.getLongToDatePHP(order.getStime(), "yyyy-MM-dd HH:mm"));
                bdHallTv.setText(order.getFyt_no());
                GlideImgUtil.glidePic(mContext, order.getErweima(), bdQrIv);
                bdTv2.setText("座位号："+showseat(order.getZuowei()));

            }

            @Override
            public void onFailed(String code, String msg, String o) {
                MyUtil.mytoast(mContext, code + "," + msg);
            }

            @Override
            public void onError(int code, String errorMsg) {
                MyUtil.mytoast(mContext, errorMsg);
            }
        });

    }

    private String showseat(List<FilmOrderDetailBean.OrderBean.ZuoweiBean> zwBean) {
        if (zwBean == null || zwBean.size() == 0) {
            return "";
        }
        String callback = "";
        for (FilmOrderDetailBean.OrderBean.ZuoweiBean ibean : zwBean) {
            callback += "\n"+ibean.getPai() + "排" + ibean.getZuo() + "座（"+(ibean.getStatus()==0?"未出票":"已出票")+"）";
        }

        return callback;
    }
}
